package com.hpradip.android.accessNumberSpeedDialer;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactsAdapter extends ArrayAdapter<Contact> {

	protected Context context;
	protected List<Contact> contacts;
	protected LayoutInflater inflater;

	public ContactsAdapter(Context context, int textViewResourceId,
			List<Contact> contacts) {
		super(context, textViewResourceId, contacts);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.contacts = contacts;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			v = inflater.inflate(R.layout.contacts_list_item, null);
		}
		String[] display_options = context.getResources().getStringArray(
				R.array.contacts_display_options_values);

		int minContacted = 0;

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);

		if (prefs
				.getString(
						context.getResources().getString(
								R.string.key_default_contacts_view), display_options[1])
				.trim().equalsIgnoreCase(display_options[0]))
			minContacted = 1;

		Contact contact = contacts.get(position);
		if (contact == null)
			return v;

//		 if( contact.getTimesCalled()<= minContacted)
//		return v;
		 
		TextView name = (TextView) v.findViewById(R.id.text_name);
		name.setText(contact.getName());

		TextView number = (TextView) v.findViewById(R.id.text_number);
		number.setText(contact.getNumber());

		TextView timesCalled = (TextView) v
				.findViewById(R.id.text_times_called);
		
		if(minContacted!=0)
		timesCalled.setText(String.valueOf(contact.getTimesCalled()));
		else
		timesCalled.setText("");

		if (contact.getPhoto() != null) {
			ImageView photo = (ImageView) v.findViewById(R.id.image_person);
			photo.setImageBitmap(contact.getPhoto());
		}
		return v;
	}

}
